<?php

require_once dirname(__FILE__) . '/../lib/BasesfGuardUserActions.class.php';

/**
 * sfGuardUser actions.
 *
 * @package    sfGuardPlugin
 * @subpackage sfGuardUser
 * @author     Fabien Potencier
 * @version    SVN: $Id: actions.class.php 12965 2008-11-13 06:02:38Z fabien $
 */
class sfGuardUserActions extends basesfGuardUserActions {

    public function executeFilter(sfWebRequest $request) {
        $this->setPage(1);

        if ($request->hasParameter('_reset')) {
            $this->setFilters($this->configuration->getFilterDefaults());

            $this->redirect('@sfGuardUser');
        }

        $this->filters = $this->configuration->getFilterForm($this->getFilters());
//var_dump($request->getParameter($this->filters->getName()));exit();
        $this->filters->bind($request->getParameter($this->filters->getName()));
        if ($this->filters->isValid()) {
            $this->setFilters($this->filters->getValues());

            $this->redirect('@sfGuardUser');
        }

        $this->pager = $this->getPager();
        $this->sort = $this->getSort();

        $this->setTemplate('index');
    }

    public function executeNew(sfWebRequest $request) {
        $this->form = $this->configuration->getForm();
        $this->sfGuardUser = $this->form->getObject();
        
    }

    public function executeActivar(sfWebRequest $request) {
        $usrId = $request->getParameter('usr_id');
        try {
            $usuario = SfGuardUserPeer::retrieveByPK($usrId);
            if (empty($usuario)) {
                throw new Exception('El usuario no existe.');
            }
            $activeState = 1;

            $usuario->setIsActive($activeState);
            $usuario->save();
        } catch (sfException $e) {
            sfContext::getInstance()->getUser()->setFlash('error', 'El usuario no existe.');
        }
        $this->redirect('@sf_guard_user');
    }

    public function executeDesactivar(sfWebRequest $request) {
        $usrId = $request->getParameter('usr_id');
        try {
            $usuario = SfGuardUserPeer::retrieveByPK($usrId);

            $inactiveState = 0;
            $usuario->setIsActive($inactiveState);
            $usuario->save();
        } catch (sfException $e) {
            sfContext::getInstance()->getUser()->setFlash('error', 'El usuario no existe.');
        }
        $this->redirect('@sf_guard_user');
    }

    protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $notice = $form->getObject()->isNew() ? 'The item was created successfully.' : 'The item was updated successfully.';

            $sfGuardUser = $form->save();

            if ($form->isNew() && !empty($sfGuardUser)) {
                $sfGuardUser->setIsActive(true);
                $sfGuardUser->save();
            }

            $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $sfGuardUser)));

            if ($request->hasParameter('_save_and_add')) {
                $this->getUser()->setFlash('notice', $notice . ' You can add another one below.');

                $this->redirect('@sfGuardUser_new');
            } else {
                $this->getUser()->setFlash('notice', $notice);

                $this->redirect(array('sf_route' => 'sfGuardUser_edit', 'sf_subject' => $sfGuardUser));
            }
        } else {
            $this->getUser()->setFlash('error', 'The item has not been saved due to some errors.', false);
        }
    }

}
